"use strict";
/* Controllers */
app.controller("schedulerListController", ['$scope', '$q', '$location', '$rootScope', '$routeParams', 'schedulerListService', function ($scope, $q, $location, $rootScope, $routeParams, schedulerListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.resolvegroupId = $routeParams.id; //  获取支持组的ID

    vm.p.startTime = getNowFormatDate(-10);
    vm.p.endTime = getNowFormatDate(20);

    var errorFn = function (result) {
        alert(result);
    };

    $scope.onSelected = function(val) {
        var vm = $scope.vm;
        if(val!=null && typeof (val) != 'undefined') {
            vm.p.resolvegroupId = val;
        }
        $scope.listScheduler();
    }

    $scope.reScheduler = function(val) {
         var vm = $scope.vm;
         if(val==null || typeof (val) == 'undefined') {
            alert('请指定需要排班的支持组!');
             return;
         }
         $q.all([$scope.reSchedRg()]).then(function () {
             $scope.listScheduler();
         });
     }
    $scope.reSchedRg = function() {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {
            startTime: getNowFormatDate(-1),
            resolvegroupId: vm.p.resolvegroupId
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功，排班从明日开始，1分钟后刷新完成！");
            } else {
                alert(response.msg);
            }
        };
        schedulerListService.reSchedRg(params, procFn, errorFn);
    }
    $scope.listScheduler = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {
            startTime: vm.p.startTime,
            endTime: vm.p.endTime,
            resolvegroupId: vm.p.resolvegroupId
        };
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        schedulerListService.listScheduler(params, procFn, errorFn);
    };

    $scope.selectRg = function() {
        var vm = $scope.vm;
        var rgId = vm.p.resolvegroupId;
        if(rgId=='0'||rgId==0) {
            return;
        }
        var rgList = vm.result.rgList;
        for(var i in rgList) {
            var it = rgList[i]
            if(rgId==it.resolvegroupId) {
                vm.rg = it;
                return;
            }
        }
    }

    $scope.listResolveGroup = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.result.rgList = res.data;
                $scope.selectRg();
            } else {
                alert(res.msg);
            }
        };
        return schedulerListService.listResolveGroup(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'schedulerId');
        $resultTable.bootstrapTable('hideColumn', 'backups');
        $resultTable.bootstrapTable('hideColumn', 'resolvegroupId');
        $q.all([$scope.listResolveGroup()]).then(function () {
            $scope.listScheduler();
        });
    };

    $scope.initializeController();

}]);


/* Services */
app.service('schedulerListService', ['ajaxService', function (ajaxService) {

    this.listScheduler = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/scheduler/listScheduler.do", successFunction, errorFunction);
    };

    this.reSchedRg = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/scheduler/reSchedRg.do", successFunction, errorFunction);
    };

    this.listResolveGroup = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/resolvegroup/listResolvegroup.do", successFunction, errorFunction);
    };

}]);

function schedulerIDOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/scheduler_add/', value, '" target="_blank">', "<u>调整</u>", '</a>'
        //'<a href="#/onduty/onduty/scheduler_add/', value,'" target="_blank">', " <u>排班</u>" , '</a>'
    ].join('');
}

function rotationTypeFmt(value, row, index) {
    if (value == null) return "-";
    switch (value) {
        case 'daily':
            return "按天";
            break;
        case 'weekly':
            return "按周";
            break;
        default:
            break;
    }
    return value;
}

function schStatusFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    if (value == 'enable') {
        return '有效'
    } else {
        return '有效'
    }
}

function dbNameOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/onduty/order/hdfs_table_list/', row.clusterName, '/', value, '" target="_blank">', "<u>", value, "</u>", '</a>'
    ].join('');
}

function startTime2WeekDayFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var dateList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    return dateList[new Date(hll2StdTime(value)).getDay()];
}

function resolvegroupSchedFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/resolvegroup_add/code/', row.resolvegroupId,'" target="_blank">', "<u>", value, "</u>", '</a>',
        '(',row.resolvegroupId,')'
    ].join('');
}


